package com.lifeng.controller;

import com.lifeng.bean.User;
import com.lifeng.client.UserServiceFeignClient;
import feign.FeignException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lifeng on 2018/4/13.
 */
@RestController
public class UserConsumerController {
    private static final Logger log = LoggerFactory.getLogger(UserConsumerController.class);
    @Autowired
    private UserServiceFeignClient userServiceFeignClient;

    @RequestMapping("/user/{id}")
    public Object getUser(@PathVariable String id){
        Object obj = null;
        try {
            obj = userServiceFeignClient.findById(id);
        }catch (FeignException e){
            log.error("调用findByIdy异常",e);
        }catch (Exception e){
            log.error("系统异常",e);
        }
        return obj;
    }

    @RequestMapping(value = "/user")
    public Object add(String id,String name){
        //return userServiceFeignClient.save(id,name);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id",id);
        map.put("name",name);
        return userServiceFeignClient.save1(map);
    }

    @RequestMapping(value = "/userVo")
    public void getUserVo(String id,String name){
        User user = new User();
        user.setId("11111111");
        System.out.println("原====>"+user);
        User user1 = userServiceFeignClient.saveUserVo(user);
        System.out.println("返回====>"+user1);
    }

    @RequestMapping(value = "/userMap")
    public void getUserMap(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("aa","是的范德萨");
        map.put("bb",111);
        System.out.println("原====>"+map);
        Map<String,Object> resMap = userServiceFeignClient.saveUserMap(map);
        System.out.println("返回====>"+resMap);
    }

    @RequestMapping(value = "/userList")
    public void getUserList(String id,String name){
        List<User> list = new ArrayList<User>();
        User user = new User();
        user.setId("11111111");
        list.add(user);
        System.out.println("原====>"+ list);
        list = userServiceFeignClient.saveUserList(list);
        System.out.println("返回====>"+ list);
    }
}
